package currenttime

import (
	"strconv"
	"strings"
	"time"
)

func FormatTimeAgo(t int64) (s string) {
	cha := time.Now().Unix() - t

	if cha < 0 {
		return
	}

	if cha < 60 {
		return "1分钟"
	}

	if cha >= 60 && cha < 60*60 {
		num := cha / 60
		result := strconv.FormatInt(num, 10) + "分钟"
		return result
	}

	if cha >= 60*60 && cha < 24*60*60 {
		num := cha / 3600
		result := strconv.FormatInt(num, 10) + "小时"
		return result
	}

	if cha >= 24*60*60 && cha < 7*24*60*60 {
		num := cha / (24 * 60 * 60)
		result := strconv.FormatInt(num, 10) + "天"
		return result
	}

	if cha >= 7*24*60*60 {
		return time.Unix(t, 0).Format("2006-01-02")
	}
	return
}

func FormatTimeAfter(t int64) (s string) {
	cha := t - time.Now().Unix()
	if cha < 0 {
		return
	}
	if cha < 60 {
		return "1分钟"
	}

	if cha >= 60 && cha < 60*60 {
		num := cha / 60
		result := strconv.FormatInt(num, 10) + "分钟"
		return result
	}

	if cha >= 60*60 && cha < 24*60*60 {
		num := cha / 3600
		result := strconv.FormatInt(num, 10) + "小时"
		return result
	}

	if cha >= 24*60*60 {
		num := cha / (24 * 60 * 60)
		result := strconv.FormatInt(num, 10) + "天"
		return result
	}

	return
}

//string 转 time.Time
//模型是什么格式 传入时间就需要什么格式
func StrToTime(timeLayout string, toBeCharge string) (t time.Time, err error) {
	//重要：获取时区
	loc, err := time.LoadLocation("Local")
	if err != nil {
		return
	}
	t, err = time.ParseInLocation(timeLayout, toBeCharge, loc)
	if err != nil {
		return
	}
	return
}

// 时间格式化：
// 日期显示方式替换，规则如下：
// 1、当前日期，前后1天内显示为今天、昨天、明天
// 例如：
// 当前日期为“2020-12-12”
// 日期为“2020-12-11 00:00”，显示为昨天 00:00
// 日期为“2020-12-12 00:00”，显示为今天 00:00
// 日期为“2020-12-13 00:00”，显示为明天 00:00
// 2、当前日期，月份相同显示xx天前/后
// 例如：
// 当前日期为“2020-12-12”
// 日期为“2020-12-01 00:00”，显示为11天前 00:00
// 日期为“2020-12-24 00:00”，显示为12天后 00:00
// 3、当前日期，年份相同显示xx（月）-xx（日）
// 例如：
// 当前日期为“2020-11-12”
// 日期为“2020-10-12 00:00”，显示为10-12 00:00
// 日期为“2020-12-12 00:00”，显示为12-12 00:00
// 4、当前日期，年份不同显示xxxx（年）-xx（月）-xx（日）
// 例如：
// 当前日期为“2020-12-12”
// 日期为“2019-12-12 00:00”，显示为2019-12-12 00:00
// 日期为“2021-12-12 00:00”，显示为2021-12-12 00:00
func DateFormat(t time.Time, layout string) (s string) {
	orgstr := t.Format(layout)
	// 分开 日期 、时间
	orgstrAttr := strings.Split(orgstr, " ")
	orgdate := orgstrAttr[0]
	orgdatetime := orgstrAttr[1]
	// 分开 年 、年-月
	orgyear := orgdate[:4]
	orgyearAmonth := orgdate[:7]

	now := time.Now()
	nowstr := now.Format(layout)
	// 分开 日期 、时间
	nowstrAttr := strings.Split(nowstr, " ")
	nowdate := nowstrAttr[0]
	// 分开 年 、年-月
	nowyear := nowdate[:4]
	nowyearAmonth := nowdate[:7]

	// 今年/其他
	if nowyear != orgyear {
		//其他年份
		s = orgstr
		return
	}

	// 本月/其他
	if nowyearAmonth != orgyearAmonth {
		//其他月份
		s = orgstr[5:]
		return
	}

	orgymd, _ := StrToTime("2006-01-02", orgdate)
	nowymd, _ := StrToTime("2006-01-02", nowdate)

	dur := orgymd.Sub(nowymd)
	var days int
	days = int(dur.Hours() / 24)

	// 今天
	if days == 0 {
		s = "今天 "
	}
	// 明天 - x天后
	if days == 1 {
		s = "明天 "
	}
	if days > 1 {
		s = strconv.Itoa(days) + "天后 "
	}
	// 昨天 - x天前
	if days == -1 {
		s = "昨天 "
	}
	if days < -1 {
		s = strconv.Itoa(-days) + "天前 "
	}

	return s + orgdatetime
}

func GetLastWeekFirstDate(d time.Time) time.Time {
	d = GetFirstDateOfWeek(d).AddDate(0, 0, 6)
	return time.Date(d.Year(), d.Month(), d.Day(), 23, 59, 59, 0, d.Location())
}

func GetFirstDateOfMonth(d time.Time) time.Time {
	d = d.AddDate(0, 0, -d.Day()+1)
	return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
}

func GetLastDateOfMonth(d time.Time) time.Time {
	d = GetFirstDateOfMonth(d).AddDate(0, 1, -1)
	return time.Date(d.Year(), d.Month(), d.Day(), 23, 59, 59, 0, d.Location())
}

func GetBeforWeekFirstDate(d time.Time) time.Time {
	d = GetBeforWeekLasttDate(d).AddDate(0, 0, -6)
	return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
}

func GetBeforWeekLasttDate(d time.Time) time.Time {
	d = GetFirstDateOfWeek(d).AddDate(0, 0, -1)
	return time.Date(d.Year(), d.Month(), d.Day(), 23, 59, 59, 0, d.Location())
}

func GetFirstDateOfWeek(d time.Time) time.Time {
	offset := int(time.Monday - d.Weekday())
	if offset > 0 {
		offset = -6
	}

	return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
}

func GetFirstDateOfYear(d time.Time) time.Time {
	return time.Date(d.Year(), time.January, 1, 0, 0, 0, 0, d.Location())
}

func GetLastDateOfYear(d time.Time) time.Time {
	return time.Date(d.Year(), time.December, 31, 0, 0, 0, 0, d.Location())
}

func GetMonthStartAndEnd(year int, month int) (start_date time.Time, end_date time.Time) {
	var m time.Month
	switch month {
	case 1:
		m = time.January
	case 2:
		m = time.February
	case 3:
		m = time.March
	case 4:
		m = time.April
	case 5:
		m = time.May
	case 6:
		m = time.June
	case 7:
		m = time.July
	case 8:
		m = time.August
	case 9:
		m = time.September
	case 10:
		m = time.October
	case 11:
		m = time.November
	case 12:
		m = time.December
	}

	start_date = time.Date(year, m, 1, 0, 0, 0, 0, time.Local)
	end_date = time.Date(year, m+1, 0, 0, 0, 0, 0, time.Local)
	return
}
